package com.HomeWork;

public class work7 {
    public static void main(String[] args) {
        //ThreadTicket threadTicket = new ThreadTicket();
        People threadTicket = new People();
        for (int i = 0; i < 10; i++) {
            new Thread(threadTicket).start();
        }

//        new Thread(threadTicket, "People2").start();
//        new Thread(threadTicket, "People3").start();
//        new Thread(threadTicket, "People4").start();
//        new Thread(threadTicket, "People5").start();
//        new Thread(threadTicket, "People6").start();
//        new Thread(threadTicket, "People7").start();
//        new Thread(threadTicket, "People8").start();
//        new Thread(threadTicket, "People9").start();
//        new Thread(threadTicket, "People10").start();
    }
}

/**
 * 8. 编写多线程程序，模拟多个人通过一座独木桥。这个座独木桥每次只能通过一个人，每个人通过独木
 * 桥的时间为5秒，有10个人同时准备过此座独木桥，显示每次通过座独木桥人的顺序。
 */
class People implements Runnable {

    @Override
    public void run() {

        synchronized (this) {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread() + "过桥");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }
}

class ThreadTicket implements Runnable {
    int ticks = 100;
    boolean f = true;

    @Override
    public void run() {

        while (f) {
            synchronized (this) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticks > 0) {
                    System.out.println(Thread.currentThread() + "买100张的第" + ticks-- + "张");
                }
                if (ticks == 0) {
                    f = false;
                }
            }
        }

    }
}
